package main

import (
	"fmt"
	"io"
	"log"
	"net"
)

func main() {
	conn,err := net.Dial("tcp","127.0.0.1:10020")
	if err != nil {
		log.Println("connect failed:",err)
		return
	}
	defer conn.Close()
	go process(conn)
	for{
		var message string
		fmt.Scan(&message)
		var writething = []byte(message)
		_,err := conn.Write(writething)
		if err != nil{
			log.Println("message scan fault:",err)
		}
	}
	}

func process(conn net.Conn)  {
	defer conn.Close()
	for {
		buf := make([]byte, 512)
		n, err := conn.Read(buf)
		if err != nil {
			if err == io.EOF{
				log.Println("connection disconnected")
			}else {
				log.Println("read failed, err:", err)
			}
			return
		}
		fmt.Printf("recieved message from user:%v >>> %v\n",conn.RemoteAddr(),string(buf[:n]))
		//fmt.Println(buf[:n])
	}

}
